package com.sean.common.utils;

import java.io.File;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.im4java.core.ConvertCmd;
import org.im4java.core.IMOperation;
import org.im4java.core.Info;
import org.im4java.core.InfoException;
import org.im4java.core.Operation;

/**
 * 图片工具类.
 * @author XinWang
 *
 */
public class ImageUtil {
	private static Log log = LogFactory.getLog(ImageUtil.class);
	
	public static String imageMagickPath = "C:\\Program Files\\ImageMagick-6.8.9-Q16";

	
	/**
	 * 根据坐标裁剪图片
	 * 
	 * @param srcPath
	 *            要裁剪图片的路径
	 * @param newPath
	 *            裁剪图片后的路径
	 * @param x1
	 *            起始横坐标
	 * @param y1
	 *            起始挫坐标
	 * @param x2
	 *            结束横坐标
	 * @param y2
	 *            结束挫坐标
	 */
	public static void cutImage(String srcPath, String newPath, int x1, int y1, int x2, int y2) {
		int width = x2 - x1;
		int height = y2 - y1;
		IMOperation op = new IMOperation();
		op.addImage(srcPath);
		/**
		 * width：裁剪的宽度 height：裁剪的高度 x：裁剪的横坐标 y：裁剪的挫坐标
		 */
		op.crop(width, height, x1, y1);
		op.addImage(newPath);
		run(op);
	}
	
	/**
	 * 根据比例坐标裁切图片.
	 * @param srcPath
	 * @param newPath
	 * @param x1
	 * @param y1
	 * @param x2
	 * @param y2
	 * @throws InfoException
	 */
	public static void cutImage(String srcPath, String newPath, float x1, float y1, float x2, float y2) throws InfoException {
		Info imgInfo = getInfo(srcPath);
		int width = imgInfo.getImageWidth();
		int height = imgInfo.getImageHeight();
		cutImage(srcPath, newPath, Math.round(width*x1), Math.round(height*y1), Math.round(width*x2), Math.round(height*y2));
	}

	/**
	 * 根据尺寸锁定比例缩放图片
	 * 
	 * @param width
	 *            缩放后的最大图片宽度
	 * @param height
	 *            缩放后的最大图片高度
	 * @param srcPath
	 *            源图片路径
	 * @param newPath
	 *            缩放后图片的路径
	 */
	public static void resizeImage(String srcPath, String newPath, Integer width, Integer height) throws Exception {
		IMOperation op = new IMOperation();
		op.addImage(srcPath);
		op.resize(width, height);
		op.addImage(newPath);
		run(op);
	}

	/**
	 * 根据宽度缩放图片.
	 * 
	 * @param width
	 *            缩放后的图片宽度
	 * @param srcPath
	 *            源图片路径
	 * @param newPath
	 *            缩放后图片的路径
	 */
	public static void resizeImageWithWidth(String srcPath, String newPath, int width)
			throws Exception {
		resizeImage(srcPath, newPath, width, null);
	}
	/**
	 * 根据高度缩放图片.
	 * @param srcPath
	 * 			源图片路径
	 * @param newPath
	 * 			缩放后图片的路径
	 * @param height
	 * 			缩放后的图片高度
	 * @throws Exception
	 */
	public static void resizeImageWithHeight(String srcPath, String newPath, int height)
			throws Exception {
		resizeImage(srcPath, newPath, null, height);
	}
	
	/**
	 * 图像旋转.
	 * @param srcPath
	 * @param newPath
	 * @param angle
	 * 			顺时针旋转角度
	 * @throws Exception
	 */
	public static void rotateImage(String srcPath, String newPath, Double angle) throws Exception {
		IMOperation op = new IMOperation();
		op.addImage(srcPath);
		op.rotate(angle);
		op.addImage(newPath);
		run(op);
	}

	/**
	 * 给图片加水印
	 * 
	 * @param srcPath
	 *            源图片路径
	 */
	public static void addImgText(String srcPath, String text) throws Exception {
		IMOperation op = new IMOperation();
		op.font("宋体").gravity("southeast").pointsize(18).fill("#BCBFC8").draw("text 5,5 " + text);
		op.addImage();
		op.addImage();
		run(op, srcPath, srcPath);
	}
	
	/**
	 * 获得图片的宽度.
	 * @param imagePath
	 * @return
	 */
	public static int getWidth(String imagePath) {
		int width = 0;
		try {
			Info imageInfo = new Info(imagePath, true);
			width = imageInfo.getImageWidth();
		} catch (InfoException e) {
			log.error(e);
		}
		return width;
	}

	/**
	 * 获得图片的高度.
	 * 
	 * @param imagePath
	 *            文件路径
	 * @return 图片高度
	 */
	public static int getHeight(String imagePath) {
		int height = 0;
		try {
			Info imageInfo = new Info(imagePath, true);
			height = imageInfo.getImageHeight();
		} catch (InfoException e) {
			log.error(e);
		}

		return height;
	}

	/**
	 * 获取图片信息
	 * @param imagePath
	 * @return
	 */
	public static Info getInfo(String imagePath) {
		try {
			return new Info(imagePath, true);
		} catch (InfoException e) {
			log.error("获取图片信息失败. imagePath:" + imagePath , e);
		}
		return null;
	}
	
//	/**
//	 * 图片缩放.
//	 * @param srcImageFile
//	 * @param destImageFile
//	 * @param width
//	 * @param height
//	 * @return
//	 */
//	public static boolean scale(String srcImageFile, String destImageFile, int width, int height) {
//		File desF = new File(destImageFile);
//		if(!desF.getParentFile().exists()) {
//			desF.getParentFile().mkdirs();
//		}
//		IMOperation op = new IMOperation();
//		op.addImage(srcImageFile);
//		op.adaptiveResize(width, height);
//		op.addImage(destImageFile);
//		return run(op);
//	}

	/**
	 * 启动处理线程.
	 * @param op
	 * @param arg1
	 * @return
	 */
	private static boolean run(Operation op, Object... arg1) {
		ConvertCmd convert = new ConvertCmd();
		// If in windows, uncomment the under line.
		if (File.separator.equals("\\")) {
			convert.setSearchPath(imageMagickPath);
		}
		try {
			convert.run(op, arg1);
		} catch (Exception e) {
			log.error("图片处理失败", e);
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public static void main(String[] args) throws Exception {
		ImageUtil.resizeImageWithWidth("C:\\Chrysanthemum.jpg", "c:\\temp.jpg", 500);
	}
}